package cn.huiyunche.base.service.framework.utils;

import cn.huiyunche.base.service.constant.QiniuConstant;
import cn.huiyunche.base.service.vo.QuniuResponseVo;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringUtils;

import java.io.File;

/**
 * 七牛工具类
 *
 * @author hdy [Tuffy]
 */
public class QiniuUtils {

    /**
     * 获取下载凭证
     *
     * @param key         文件key
     * @param treatMethod 处理管道
     * @return 下载凭证
     */
    public static String generateDownloadTicket(String key, String treatMethod) {
        //七牛api地址：http://developer.qiniu.com/docs/v6/sdk/java-sdk.html#private-download
        Auth auth = Auth.create(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_ACCESS_KEY),
                HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_SECRET_KEY));
        long invalidationTime = 0L;
        invalidationTime = HYCUtils.getConstantPropertiesLoader().getInteger(QiniuConstant.QINIU_INVALIDATION_TIME);
        String url = HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_DOWNLOAD_ADDRESS);
        return auth.privateDownloadUrl(url + "/" + key + treatMethod, invalidationTime);
    }

    /**
     * 获取下载凭证
     *
     * @param key         文件key
     * @param treatMethod 处理管道
     * @return 下载凭证
     */
    public static String generateDownloadTicket(String bucket, String key, String treatMethod, String w, String h) {
        //七牛api地址：http://developer.qiniu.com/docs/v6/sdk/java-sdk.html#private-download
        Auth auth = Auth.create(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_ACCESS_KEY),
                HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_SECRET_KEY));
        long invalidationTime = 0L;
        invalidationTime = HYCUtils.getConstantPropertiesLoader().getInteger(QiniuConstant.QINIU_INVALIDATION_TIME);
        if (!StringUtils.isNullOrEmpty(treatMethod)) {
            if (!StringUtils.isNullOrEmpty(w)) {
                treatMethod = treatMethod + "/w/" + w;
            }
            if (!StringUtils.isNullOrEmpty(h)) {
                treatMethod = treatMethod + "/h/" + h;
            }
            if (!StringUtils.isNullOrEmpty(treatMethod)) {
                treatMethod = "?imageView2/" + treatMethod + treatMethod;
            }
        }
        String urlKey = QiniuConstant.QINIU_DOWNLOAD_ADDRESS;
        /*if(!StringUtils.isNullOrEmpty(bucket)
				&& bucket.equals(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_BUCKET))){
			urlKey = QiniuConstant.QINIU_HTML_ADDRESS;
		} else */
        if (!StringUtils.isNullOrEmpty(bucket)
                && bucket.equals(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_LOGO_BUCKET))) {
            urlKey = QiniuConstant.QINIU_LOGO_DOWNLOAD_ADDRESS;
        } else if (!StringUtils.isNullOrEmpty(bucket)
                && bucket.equals(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_BANK_LOGO_BUCKET))) {
            urlKey = QiniuConstant.QINIU_BANK_LOGO_DOWNLOAD_ADDRESS;
        }
        String url = HYCUtils.getConstantPropertiesLoader().getProperty(urlKey);
        return auth.privateDownloadUrl(url + "/" + key + treatMethod, invalidationTime);
    }

    /**
     * 获取上传凭证key
     *
     * @return 返回凭证key
     */
    public static String generateSimpleUploadTicket() {
        //api地址：http://developer.qiniu.com/docs/v6/sdk/java-sdk.html
        Auth auth = Auth.create(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_ACCESS_KEY),
                HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_SECRET_KEY));
        long invalidationTime = 0L;
        invalidationTime = HYCUtils.getConstantPropertiesLoader().getInteger(QiniuConstant.QINIU_INVALIDATION_TIME);
        return auth.uploadToken(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_BUCKET), null, invalidationTime, null);
    }

    /**
     * 根据Bucket获取上传凭证key
     *
     * @return 返回凭证key
     */
    public static String generateSimpleUploadTicket(String bucket) {
        //api地址：http://developer.qiniu.com/docs/v6/sdk/java-sdk.html
        Auth auth = Auth.create(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_ACCESS_KEY),
                HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_SECRET_KEY));
        long invalidationTime = 0L;
        invalidationTime = HYCUtils.getConstantPropertiesLoader().getInteger(QiniuConstant.QINIU_INVALIDATION_TIME);
        return auth.uploadToken(HYCUtils.getConstantPropertiesLoader().getProperty(bucket), null, invalidationTime, null);
    }


    /**
     * 流文件上传
     *
     * @param file  文件对象
     * @param key   文件key
     * @param token 凭证
     * @throws QiniuException
     */
    public static QuniuResponseVo upload(byte[] data, String key) throws QiniuException {
        UploadManager uploadManager = new UploadManager();
        Response res = uploadManager.put(data, key, generateSimpleUploadTicket());
        return res.jsonToObject(QuniuResponseVo.class);
    }

    /**
     * 文件上传
     *
     * @param file  文件对象
     * @param key   文件key
     * @param token 凭证
     * @throws QiniuException
     */
    public static QuniuResponseVo upload(File file, String key) throws QiniuException {
        UploadManager uploadManager = new UploadManager();
        Response res = uploadManager.put(file, key, generateSimpleUploadTicket());
        return res.jsonToObject(QuniuResponseVo.class);
    }

    /**
     * 获取微信发送消息图片url
     *
     * @param orderType 订单类型(暂时没有判断，等待ui设计)
     * @return
     */
    public static String getWeixinOrderMsgPicUrl(int orderType) {
        return QiniuUtils.generateDownloadTicket("huiyunchelogo.jpg", "");
    }

}
